/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ComputerGui.java
 *
 * Created on 17/06/2011, 11:58:56 AM
 */

package GUI.ATTENDANT;

import Procedures.DBConection;
import entities.Computer;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author SIX-14
 */
public class ComputerGui extends javax.swing.JPanel {
    /** Creates new form ComputerGui */
    public ComputerGui(DBConection myConection, int action, Computer computer, JFrame patern) {
        initComponents();
        setMyConection(myConection);
        setAction(action);
        setComputer(computer);
        initComponents2();
        setPaternFrame(patern);

    }

    private DBConection myConection;
    private int action;
    private Computer computer;
    private JFrame paternFrame;

    public JFrame getPaternFrame() {
        return paternFrame;
    }

    public void setPaternFrame(JFrame paternFrame) {
        this.paternFrame = paternFrame;
    }


    public int getAction() {
        return action;
    }

    public void setAction(int action) {
        this.action = action;
    }

    public Computer getComputer() {
        return computer;
    }

    public void setComputer(Computer computer) {
        this.computer = computer;
    }

    public DBConection getMyConection() {
        return myConection;
    }

    public void setMyConection(DBConection myConection) {
        this.myConection = myConection;
    }

    public void fillComboBoxes(){
        getMyConection().sendSQLQuery("SELECT LETTER FROM COMPUTER_ROOM where active=1;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox1.removeAllItems();
            jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }
        getMyConection().sendSQLQuery("SELECT NAME FROM STATE_COMPUTER;");
        elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            jComboBox2.removeAllItems();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(elements));
        }

    }

    public void assignPort(){
        /*
        getMyConection().sendSQLQuery("SELECT PORT FROM COMPUTER ORDER BY PORT ASC;");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements != null) {
            if(elements.length==1){
                jTextField3.setText(String.valueOf(Integer.parseInt(elements[0])+1));
            }else{
                for(int i=1; i<elements.length; i++){
                    if((Integer.parseInt(elements[i])-Integer.parseInt(elements[i-1]))>1){
                        jTextField3.setText(String.valueOf(Integer.parseInt(elements[i-1])+1));
                        break;
                    }else{
                        if(i==elements.length-1){
                            jTextField3.setText(String.valueOf(Integer.parseInt(elements[i])+1));
                        }
                    }
                }
            }

        }else{
            jTextField3.setText("1025");
        }*/
        jTextField3.setText("8490");


    }/*
    public boolean verifierPort(){
        getMyConection().sendSQLQuery("SELECT PORT FROM COMPUTER WHERE PORT="+jTextField3.getText()+";");
        String[] elements = getMyConection().getLastResultSetColumnStrings(1);
        if (elements == null) {
            System.out.println("se puede cambiar el puerto");
        }else{
            System.out.println("no se puede poner ese puerto");
        }


        return true;
    }*/

    public void initComponents2(){
        fillComboBoxes();
        switch(getAction()){
            case 1: jButton1.setText("Agregar");  assignPort(); jComboBox2.setSelectedItem("Agregar"); jComboBox2.setEnabled(false);break;
            case 2: jButton1.setText("Modificar"); jTextField3.setEditable(true); fillfield(); break;
            case 3: jButton1.setText("Eliminar"); fillfield(); break;
        }
    }
    public void fillfield(){
        jTextField1.setText(String.valueOf(getComputer().getNumber()));
        jComboBox1.setSelectedItem(getComputer().getLetterComputerRoom());
        getMyConection().sendSQLQuery("SELECT NAME FROM STATE_COMPUTER WHERE ID="+getComputer().getState()+";");
        jComboBox2.setSelectedItem(getMyConection().getLastResultSetColumnStrings(1)[0]);
        jTextField2.setText(getComputer().getIp());
        if(getAction()==2){
            jTextField3.setText(String.valueOf(getComputer().getPort()));
        }else{
            jLabel5.setText(jLabel5.getText()+" "+getComputer().getPort());
        }


    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();

        jLabel1.setText("Numero de computadora: ");

        jLabel2.setText("Sala donde se encuentra:");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel3.setText("Estado de la computadora:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        jLabel4.setText("Ip computadora:");

        jLabel5.setText("Puerto que usara este equipo:");

        jButton1.setText("jButton1");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jTextField3.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox1, 0, 100, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox2, 0, 94, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)))
                .addGap(175, 175, 175))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jButton1)
                .addContainerGap(67, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        if(jButton1.getText().equals("Agregar")){
            getMyConection().sendSQL("INSERT INTO COMPUTER SET NUMBER="+jTextField1.getText()+", LETTERCOMPUTERROOM='"+jComboBox1.getSelectedItem().toString()+"', STATE=(SELECT ID FROM STATE_COMPUTER WHERE NAME='"+jComboBox2.getSelectedItem().toString()+"'), IP='"+jTextField2.getText()+"', PORT="+jTextField3.getText()+";");
            JOptionPane.showMessageDialog(null, "Computadora Registrada Exitosamente", "Registro Exitoso", 1);

        }else if(jButton1.getText().equals("Modificar")){
            //verifierPort();
            getMyConection().sendSQL("UPDATE COMPUTER SET NUMBER="+jTextField1.getText()+", LETTERCOMPUTERROOM='"+jComboBox1.getSelectedItem().toString()+"', STATE=(SELECT ID FROM STATE_COMPUTER WHERE NAME='"+jComboBox2.getSelectedItem().toString()+"'), IP='"+jTextField2.getText()+"', PORT="+jTextField3.getText()+" WHERE NUMBER="+getComputer().getNumber()+";");          
            JOptionPane.showMessageDialog(null, "Computadora modificada Exitosamente", "Computadora Modificada", 1);
            this.setVisible(false);
            getPaternFrame().remove(this);

        }else if(jButton1.getText().equals("Eliminar")){
            getMyConection().sendSQL("UPDATE COMPUTER SET STATE=4 WHERE NUMBER="+jTextField1.getText());
            JOptionPane.showMessageDialog(null, "Computadora dada de Baja Exitosamente", "Computadora Baja", 1);
            this.setVisible(false);
            getPaternFrame().remove(this);

        }
    }//GEN-LAST:event_jButton1MouseClicked


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables

}
